<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manage:position:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:position:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:position:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manage:position:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="positionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模板序号" align="center" prop="id" />
      <el-table-column label="模板名称" align="center" prop="name" />
<!--      <el-table-column label="二维码位置(x)" align="center" prop="qrCodeX" />
      <el-table-column label="二维码位置(y)" align="center" prop="qrCodeY" />
      <el-table-column label="二维码大小" align="center" prop="qrCodeSize" />
      <el-table-column label="文本位置x" align="center" prop="textX" />
      <el-table-column label="文本位置y" align="center" prop="textY" />
      <el-table-column label="文本间隔" align="center" prop="textInterval" />
      <el-table-column label="字体宽" align="center" prop="typefaceWidth" />
      <el-table-column label="字体高" align="center" prop="typefaceHeight" />-->
      <el-table-column label="创建时间" align="center" prop="gmtCreate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtCreate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="gmtModify" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtModify, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:position:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:position:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改打印位置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-col :span="24">
          <el-form-item label="模板名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入模板名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纸张宽度mm" prop="pageWidth">
            <el-input-number :precision="2" v-model="form.pageWidth" placeholder="请输入纸张宽度(mm)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纸张高度mm" prop="pageHeight">
            <el-input-number :precision="2" v-model="form.pageHeight" placeholder="请输入纸张高度(mm)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="二维码位置(x)" prop="qrCodeX">
            <el-input-number :precision="2" v-model="form.qrCodeX" placeholder="请输入二维码位置(x)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="二维码位置(y)" prop="qrCodeY">
            <el-input-number :precision="2" v-model="form.qrCodeY" placeholder="请输入二维码位置(y)" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="二维码大小" prop="qrCodeSize">
            <el-input-number :precision="2" v-model="form.qrCodeSize" placeholder="请输入二维码大小" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="文本位置(x)" prop="textX">
            <el-input-number :precision="2" v-model="form.textX" placeholder="请输入文本位置x" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="文本位置(y)" prop="textY">
            <el-input-number :precision="2" v-model="form.textY" placeholder="请输入文本位置y" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="文本间隔" prop="textInterval">
            <el-input-number :precision="2" v-model="form.textInterval" placeholder="请输入文本间隔" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字体大小" prop="fontsize">
            <el-input-number :precision="2" v-model="form.fontsize" placeholder="请输入字体大小" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字体宽" prop="typefaceWidth">
            <el-input-number :precision="2" v-model="form.typefaceWidth" placeholder="请输入字体宽" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="字体高" prop="typefaceHeight">
            <el-input-number :precision="2" v-model="form.typefaceHeight" placeholder="请输入字体高" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标题位置(x)" prop="titleX">
            <el-input-number :precision="2" v-model="form.titleX" placeholder="请输入标题位置(x)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标题位置(y)" prop="titleY">
            <el-input-number :precision="2" v-model="form.titleY" placeholder="请输入标题位置(y)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="标题大小" prop="titlesize">
            <el-input-number :precision="2" v-model="form.titlesize" placeholder="请输入标题大小" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPosition, getPosition, delPosition, addPosition, updatePosition } from "@/api/manage/position";

export default {
  name: "Position",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 打印位置表格数据
      positionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        qrCodeX: null,
        qrCodeY: null,
        qrCodeSize: null,
        textX: null,
        textY: null,
        textInterval: null,
        typefaceWidth: null,
        typefaceHeight: null,
        gmtCreate: null,
        gmtModify: null,
        isDelete: null
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "模板名称不能为空", trigger: "blur" }
        ],
        qrCodeX: [
          { required: true, message: "二维码位置(x)不能为空", trigger: "blur" }
        ],
        qrCodeY: [
          { required: true, message: "二维码位置(y)不能为空", trigger: "blur" }
        ],
        qrCodeSize: [
          { required: true, message: "二维码大小不能为空", trigger: "blur" }
        ],
        textX: [
          { required: true, message: "文本位置x不能为空", trigger: "blur" }
        ],
        textY: [
          { required: true, message: "文本位置y不能为空", trigger: "blur" }
        ],
        textInterval: [
          { required: true, message: "文本间隔不能为空", trigger: "blur" }
        ],
        typefaceWidth: [
          { required: true, message: "字体宽不能为空", trigger: "blur" }
        ],
        typefaceHeight: [
          { required: true, message: "字体高不能为空", trigger: "blur" }
        ],
        pageWidth: [
          { required: true, message: "纸张宽度(mm)不能为空", trigger: "blur" }
        ],
        pageHeight: [
          { required: true, message: "纸张高度(mm)不能为空", trigger: "blur" }
        ],
        fontsize: [
          { required: true, message: "字体大小不能为空", trigger: "blur" }
        ],
        titleX: [
          { required: true, message: "标题X不能为空", trigger: "blur" }
        ],
        titleY: [
          { required: true, message: "标题Y不能为空", trigger: "blur" }
        ],
        titlesize: [
          { required: true, message: "标题大小不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询打印位置列表 */
    getList() {
      this.loading = true;
      listPosition(this.queryParams).then(response => {
        this.positionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        qrCodeX: null,
        qrCodeY: null,
        qrCodeSize: null,
        textX: null,
        textY: null,
        textInterval: null,
        typefaceWidth: null,
        typefaceHeight: null,
        gmtCreate: null,
        gmtModify: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加打印位置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPosition(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改打印位置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePosition(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPosition(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除打印位置编号为"' + ids + '"的数据项？').then(function() {
        return delPosition(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/position/export', {
        ...this.queryParams
      }, `position_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
